//
//  EFSubmitOrderViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/28.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFSubmitOrderViewController.h"
#import "EFTransHomeDetailViewItemTime.h"
#import "EFTransHomeDetailViewItemPrice.h"
#import "EFTransHomeDetailViewItemMark.h"
#import "EFTransHomeDetailViewCellMark.h"
#import "EFTransHomeDetailViewItemTotalPrice.h"
#import "EFOrderUserInfoItem.h"
#import "EFOrderProductItem.h"
#import "EFSepecialItem.h"
#import "EFMyDeliveryAddressViewController.h"
#import "EFCouponViewController.h"
#import "EFPayTypeChooseViewController.h"
#import "EFShoppingCartItem.h"
#import "EFUsePointsViewController.h"
#import "EFOrderOpenItem.h"
#import "iToast.h"
#import "WXApi.h"
#import "EFOrderDetailViewController.h"
#import "EFUserLocationItem.h"
#import "TPKeyboardAvoidingScrollView.h"

@interface EFSubmitOrderViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)NSMutableArray* dataArray;
@property(strong,nonatomic)UITableView* tableView;

@property(strong,nonatomic)UIView* bottomView;
@property(strong,nonatomic)UILabel* totalPriceLabel;
@property(strong,nonatomic)UIButton* payBtn;

@property (strong, nonatomic) TPKeyboardAvoidingScrollView * scrollView;

@property(strong,nonatomic)NSMutableArray* goodsArray;

@property(strong,nonatomic)NSString* productList;
@property(strong,nonatomic)NSString* postage;
@property(strong,nonatomic)NSString* couponId;
@property(strong,nonatomic)NSString* totalPrice;
@property(strong,nonatomic)NSString* memberPoints;
@property(strong,nonatomic)NSString* deductAmount;
@property(strong,nonatomic)NSString* discount;
@property(strong,nonatomic)NSString* payAmount;   //实付款
@property(strong,nonatomic)NSString* mark;   //备注
@property(strong,nonatomic)UITextField* markTF;   //备注


@property (copy, nonatomic) NSString *orderNo;
@property(assign,nonatomic)Boolean showMore;

@end

@implementation EFSubmitOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"订单";
        self.mark = @"";
        self.dataArray = [NSMutableArray array];
        self.goodsArray = [NSMutableArray array];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeDefaultAddress) name:IC_CHANGE_DEFAULT_NOTIFICATION object:nil];
        
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeCoupon:) name:IC_CHANGE_COUPON_NOTIFICATION object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changePoints:) name:IC_CHANGE_POINTS_NOTIFICATION object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onWeixinPayDoneNotification:) name:kNotificationWeixinPaySuccess object:nil];
    }
    return self;
}

-(void)changeDefaultAddress{
    EFMyDeliveryAddressViewItem* defaultAddres  = APP_CONTEXT.addressManager.defaultAddress;
    EFOrderUserInfoItem *userInfItem = [[EFOrderUserInfoItem alloc] initWithMyDefaultAddress:defaultAddres];
    
    NSMutableString* productList = [[NSMutableString alloc] init];
    for(int i = 0 ; i < self.goodsArray.count ; ++i){
        EFOrderProductItem* productItem = [self.goodsArray objectAtIndex:i];
        if(i == self.goodsArray.count -1){
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }else{
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@|",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }
    }
    
    [APP_CONTEXT.net changeOrderAddress:productList addressId:defaultAddres.id OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            [self.dataArray replaceObjectAtIndex:0 withObject:userInfItem];
            [self.tableView reloadData];
//            if ([info objectForKey:@"rData"] ){
//                NSNumber* rData = [info objectForKey:@"rData"];
//                if(rData != nil  &&  rData.intValue == 1){
//                    [self.dataArray replaceObjectAtIndex:0 withObject:userInfItem];
//                    [self.tableView reloadData];
//                }else{
//                    [self.navigationController popViewControllerAnimated:YES];
//                }
//            }
                
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}


-(void)changeCoupon:(NSNotification*)notification{
    [self.dataArray removeAllObjects];
    NSString* couponId = (NSString*)notification.object;
    
    EFMyDeliveryAddressViewItem* defaultAddres  = APP_CONTEXT.addressManager.defaultAddress;
    EFOrderUserInfoItem *userInfItem = [[EFOrderUserInfoItem alloc] initWithMyDefaultAddress:defaultAddres];
    
    [self.dataArray addObject:userInfItem];
    
    float totalPrice = 0;
    NSMutableString* productList = [[NSMutableString alloc] init];
    for(int i = 0 ; i < self.goodsArray.count ; ++i){
        EFOrderProductItem* productItem = [self.goodsArray objectAtIndex:i];
        if(i == self.goodsArray.count -1){
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }else{
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@|",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }
        
        totalPrice += productItem.price.integerValue * productItem.qty.integerValue;
        if(self.showMore){
            if(i < ORDER_MAX_SHOW_COUNT){
                [self.dataArray addObject:productItem];
            }
        }else{
            [self.dataArray addObject:productItem];
        }
    }
    //添加展开cell
    if (self.goodsArray.count > ORDER_MAX_SHOW_COUNT && self.showMore) {
        EFOrderOpenItem *openItem = [[EFOrderOpenItem alloc] init];
        openItem.count = self.goodsArray.count - ORDER_MAX_SHOW_COUNT;
        [self.dataArray addObject:openItem];
    }
    self.productList = productList;
    
    [self showHUD];
    NSNumber* type = [[NSNumber alloc] initWithInt:0];
    NSNumber* carrier = nil;
    [APP_CONTEXT.net changeCoupon:couponId productList:productList addressID:defaultAddres.id discount:self.discount type:type carrier:carrier OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [self.HUD hide:YES];
        if(code == 0){
            [self setPayData:info totalPrice:totalPrice];
            
        }else{
            [[iToast makeText:message] show];
        }
    }];
}

-(void)changePoints:(NSNotification*)notification{
    self.memberPoints = (NSString*)notification.object;
    if(self.memberPoints.length <= 0){
        return;
    }
    
    [self.dataArray removeAllObjects];
    
    EFMyDeliveryAddressViewItem* defaultAddres  = APP_CONTEXT.addressManager.defaultAddress;
    EFOrderUserInfoItem *userInfItem = [[EFOrderUserInfoItem alloc] initWithMyDefaultAddress:defaultAddres];
    
    [self.dataArray addObject:userInfItem];
    
    float totalPrice = 0;
    NSMutableString* productList = [[NSMutableString alloc] init];
    for(int i = 0 ; i < self.goodsArray.count ; ++i){
        EFOrderProductItem* productItem = [self.goodsArray objectAtIndex:i];
        if(i == self.goodsArray.count -1){
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }else{
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@|",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }
        
        totalPrice += productItem.price.integerValue * productItem.qty.integerValue;
        if(self.showMore){
            if(i < ORDER_MAX_SHOW_COUNT){
                [self.dataArray addObject:productItem];
            }
        }else{
            [self.dataArray addObject:productItem];
        }
    }
    //添加展开cell
    if (self.goodsArray.count > ORDER_MAX_SHOW_COUNT && self.showMore) {
        EFOrderOpenItem *openItem = [[EFOrderOpenItem alloc] init];
        openItem.count = self.goodsArray.count - ORDER_MAX_SHOW_COUNT;
        [self.dataArray addObject:openItem];
    }
    
    NSNumber* type = [[NSNumber alloc] initWithInt:0];
    NSNumber* carrier = nil;
    
    [self showHUD];
    self.productList = productList;
    [APP_CONTEXT.net changePoints:self.memberPoints productList:productList addressID:defaultAddres.id couponid:self.couponId discount:self.discount type:type carrier:carrier OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [self.HUD hide:YES];
        if(code == 0){
            [self setPayData:info totalPrice:totalPrice];
            
        }else{
            [[iToast makeText:message] show];
        }
    }];
}


-(instancetype)initWithData:(NSMutableArray *)array{
    self = [self init];
    if(self){
        for ( EFShoppingCartItem *shopCartItem in array) {
            EFOrderProductItem* productItem = [[EFOrderProductItem alloc] initWithShoppingCartItem:shopCartItem];
            [self.goodsArray addObject:productItem];
        }
    }
    return self;
} 

-(void)initView{
    
    [self.view addSubview:self.scrollView];
    
    [self.bottomView addSubview:self.totalPriceLabel];
    [self.bottomView addSubview:self.payBtn];
    [self.scrollView addSubview:self.bottomView];
    [self.scrollView addSubview:self.tableView];
}

-(void)initData{
    [self showHUD];
    self.tableView.hidden = YES;
    self.bottomView.hidden = YES;
    EFMyDeliveryAddressViewItem* defaultAddres  = APP_CONTEXT.addressManager.defaultAddress;
    EFOrderUserInfoItem *userInfItem = [[EFOrderUserInfoItem alloc] initWithMyDefaultAddress:defaultAddres];
    
    [self.dataArray addObject:userInfItem];     
    if (self.goodsArray.count > ORDER_MAX_SHOW_COUNT) {
        self.showMore = true;
    }else{
        self.showMore = false;
    }
    
    float totalPrice = 0;
    NSMutableString* productList = [[NSMutableString alloc] init];
    for(int i = 0 ; i < self.goodsArray.count ; ++i){
        EFOrderProductItem* productItem = [self.goodsArray objectAtIndex:i];
        if(i == self.goodsArray.count -1){
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }else{
            NSString* tempItem = [NSString stringWithFormat:@"%@,%@|",productItem.id,productItem.qty];
            [productList appendString:tempItem];
        }
        
        totalPrice += productItem.price.floatValue * productItem.qty.integerValue;
        if(self.showMore){
            if(i < ORDER_MAX_SHOW_COUNT){
                [self.dataArray addObject:productItem];
            }
        }else{
            [self.dataArray addObject:productItem];
        }
    }
    //添加展开cell
    if (self.goodsArray.count > ORDER_MAX_SHOW_COUNT && self.showMore) {
        EFOrderOpenItem *openItem = [[EFOrderOpenItem alloc] init];
        openItem.count = self.goodsArray.count - ORDER_MAX_SHOW_COUNT;
        [self.dataArray addObject:openItem];
    }
    
    EFUserLocationItem* location = APP_CONTEXT.locationManager.pinLocation;
    NSString* position =  [NSString stringWithFormat:@"%lf,%lf",location.userCoordinate.longitude,location.userCoordinate.latitude];
    
    
    NSNumber* type = [[NSNumber alloc] initWithInt:0];
    NSNumber* carrier = nil;
    
    self.productList = productList;
    [APP_CONTEXT.net createOrder:productList addressID:defaultAddres.id position:position type:type carrier:carrier OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [self.HUD hide:YES];
        if(code == 0){
            self.tableView.hidden = NO;
            self.bottomView.hidden = NO;
            [self setPayData:info totalPrice:totalPrice];
        }else{
            [[iToast makeText:message] show];
        }
    }];
    
    
//    EFTransHomeDetailViewItemTime* itemOne = [[EFTransHomeDetailViewItemTime alloc] init];
//    itemOne.name = @"收货时间";
//    itemOne.status = @"周日15：00";
//    itemOne.type = 0;
//    [self.dataArray addObject:itemOne];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    if([item isKindOfClass:[EFOrderUserInfoItem class]]){
        EFMyDeliveryAddressViewController *address = [[EFMyDeliveryAddressViewController alloc] init];
        [ICNavigator pushViewController:address animated:YES];
    }else if([item isKindOfClass:[EFTransHomeDetailViewItemTime class]]){
        EFTransHomeDetailViewItemTime* priceItem = (EFTransHomeDetailViewItemTime*)item;
        if(priceItem.type == 1){
            EFPayTypeChooseViewController *payType = [[EFPayTypeChooseViewController alloc] init];
            [ICNavigator pushViewController:payType animated:YES];
        }else if(priceItem.type == 2){
            EFCouponViewController *coupon = [[EFCouponViewController alloc] initWithType:1 totlaPrice:self.totalPrice];
            [ICNavigator pushViewController:coupon animated:YES];
        }else if(priceItem.type == 3){
            EFUsePointsViewController *points = [[EFUsePointsViewController alloc] initWithTotalPirce:self.totalPrice couponId:self.couponId postage:self.postage discount:self.discount];
            [ICNavigator pushViewController:points animated:YES];
        }
    }else if([item isKindOfClass:[EFOrderOpenItem class]]){
        self.showMore = false;
        [self extendArray];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item = self.dataArray[indexPath.row];
    if ([item isKindOfClass:[EFOrderUserInfoItem class]]) {
        return 70;
    }
    return item.viewHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem* item = self.dataArray[indexPath.row];
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    
    if([item isKindOfClass:[EFTransHomeDetailViewItemMark class]]){
        self.markTF =  ((EFTransHomeDetailViewCellMark*)cell).markTF;
        ((EFTransHomeDetailViewCellMark*)cell).markblock = ^(NSString* mark){
            self.mark = mark; 
        };

    }
    
    return cell;
}

- (void) hideKeyboard {
    [self.markTF resignFirstResponder];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight- 50 )];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.top =   kStatusBarHeight;
        _tableView.bottom = self.bottomView.top - 5;
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
        gestureRecognizer.numberOfTapsRequired = 1;
        gestureRecognizer.cancelsTouchesInView = NO;
        [self.tableView addGestureRecognizer:gestureRecognizer];
        
    }
    return _tableView;
}

-(UIView *)bottomView{
    if(_bottomView == NULL){
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        _bottomView.top = kScreenHeight - kStatusBarHeight - 50;
    }
    return _bottomView;
}

-(UILabel *)totalPriceLabel{
    if(_totalPriceLabel == NULL){
        _totalPriceLabel = [[UILabel alloc]init];
        _totalPriceLabel.textColor = kBlack333;
        _totalPriceLabel.font = [UIFont systemFontOfSize:15];
        _totalPriceLabel.top = 14;
    }
    return _totalPriceLabel;
}

-(UIButton *)payBtn{
    if(_payBtn == NULL){
        _payBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120, 50)];
        _payBtn.backgroundColor = RGBCOLOR(0x69BA45);
        _payBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _payBtn.titleLabel.textColor = [UIColor whiteColor];
        _payBtn.left = kScreenWidth - 120;
        [_payBtn setTitle:@"提交订单" forState:UIControlStateNormal];
        [_payBtn addTarget:self action:@selector(onClickPay) forControlEvents:UIControlEventTouchUpInside];
    }
    return _payBtn;
}

-(TPKeyboardAvoidingScrollView*)scrollView{
    if(_scrollView == NULL){
        _scrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame: CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _scrollView.contentSize =  CGSizeMake(kScreenWidth, kScreenHeight - kStatusBarHeight);
        
    }
    return _scrollView;
}

-(void)onClickPay{
    [self showHUD];
    
    NSNumber* type = [[NSNumber alloc] initWithInt:0];
    NSNumber* carrier = nil;
    EFMyDeliveryAddressViewItem* defaultAddres  = APP_CONTEXT.addressManager.defaultAddress;
    [APP_CONTEXT.net placeOrderProductList:self.productList addressID:defaultAddres.id paymentMethod:[NSNumber numberWithInt:1] couponId:self.couponId points:self.memberPoints userComment:self.mark type:type carrier:carrier OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            [self updateShopCart];
            if(self.payAmount.intValue == 0){
                if (info[@"orderNo"] != nil) {
                    self.orderNo = info[@"orderNo"];
                    EFOrderDetailViewController *vc = [[EFOrderDetailViewController alloc] initWithOrderNumber:self.orderNo];
                    [ICNavigator pushViewController:vc animated:YES];
                }
            }else{
                [self handleWeixinPayActionResponse:info];
            }
        }else{
            [self.HUD hide:YES];
            [[iToast makeText:message]show];
        }
    }];
}

#pragma mark --WeiXinPay

- (void)handleWeixinPayActionResponse:(NSDictionary *)result {
    //    if (![WXApi isWXAppInstalled]) {
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"请安装微信并登录您的帐号" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    //        [alert show];
    //        return;
    //    }
    //    if (![WXApi isWXAppSupportApi]) {
    //        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"您的微信版本不支持开放平台,请更新您的微信版本!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    //        [alert show];
    //        return;
    //    }
    
    [self.HUD hide:YES];
    NSString *noncestr = result[@"noncestr"];
    NSString *package = @"Sign=WXPay";// result[@"package"];//@"Sign=WXPay";
    NSString *prepayId = result[@"prepayid"];
    NSString *partnerId = result[@"partnerid"];
    NSString *sign = result[@"sign"];
    NSString *timeStamp = result[@"timestamp"];
    if (result[@"orderNo"] != nil) {
        self.orderNo = result[@"orderNo"];
    }
    PayReq *request = [[PayReq alloc] init];
    request.partnerId = partnerId;
    request.prepayId = prepayId;
    request.package = package;
    request.nonceStr= noncestr;
    request.timeStamp = [timeStamp intValue];
    request.sign= sign;
    
    [WXApi sendReq:request];
}

- (void)onWeixinPayDoneNotification:(NSNotification *)notification{
    [self onWeixinPaymentResult:notification.object];
}

- (void)onWeixinPaymentResult:(PayResp *)resp {
    NSInteger statusCode = resp.errCode;
    if (statusCode == 0) {
        [self onPayDone:EWXinPay];
    } else if (statusCode != -2){
        [[iToast makeText:resp.errStr] show];
    } else {
        [[iToast makeText:@"支付不成功"] show];
        //跳转订单详情页
        EFOrderDetailViewController *vc = [[EFOrderDetailViewController alloc] initWithOrderNumber:self.orderNo];
        [ICNavigator pushViewController:vc animated:YES];
    }
}

- (void)onPayDone:(NSInteger)payType{
    [[iToast makeText:@"您的订单支付成功"] show];
    //跳转订单详情页
    EFOrderDetailViewController *vc = [[EFOrderDetailViewController alloc] initWithOrderNumber:self.orderNo];
    [ICNavigator pushViewController:vc animated:YES];
}

-(void)setPayData:(NSDictionary*) info totalPrice:(CGFloat)totalPrice{
    //CGFloat realPay = totalPrice ;
    self.totalPrice = [NSString stringWithFormat:@"%f",totalPrice];
    EFTransHomeDetailViewItemTime* itemTwo = [[EFTransHomeDetailViewItemTime alloc] init];
    itemTwo.name = @"支付方式";
    itemTwo.status = @"微信支付";
    itemTwo.type = 1;
    [self.dataArray addObject:itemTwo];
    if ([info objectForKey:@"couponId"] ){
        self.couponId = [info objectForKey:@"couponId"];
    }
    
    
    if ([info objectForKey:@"couponName"] ){
        NSString *couponName = [info objectForKey:@"couponName"];
        EFTransHomeDetailViewItemTime* itemCouponName = [[EFTransHomeDetailViewItemTime alloc] init];
        itemCouponName.name = @"优惠券";
        itemCouponName.status = couponName;
        itemCouponName.type = 2;
        [self.dataArray addObject:itemCouponName];
    }
    
    if ([info objectForKey:@"memberPoints"] ){
        NSString *memberPoints = [info objectForKey:@"memberPoints"];
        self.memberPoints = memberPoints;
        EFTransHomeDetailViewItemTime* itemPoints = [[EFTransHomeDetailViewItemTime alloc] init];
        itemPoints.name = @"积分抵扣";
        itemPoints.status = memberPoints;
        itemPoints.type = 3;
        [self.dataArray addObject:itemPoints];
        
    }
    EFTransHomeDetailViewItemMark* itemMark = [[EFTransHomeDetailViewItemMark alloc] init];
    itemMark.name = @"备注";
    itemMark.mark = self.mark;
    [self.dataArray addObject:itemMark];
    
    NSString* strTotalPrice = @"";
    if([info objectForKey:@"totalPrice"]){
        strTotalPrice = [info objectForKey:@"totalPrice"];
        self.totalPrice = strTotalPrice;
    }
    
    EFTransHomeDetailViewItemPrice* totalPriceItem = [[EFTransHomeDetailViewItemPrice alloc] init];
    totalPriceItem.name = @"商品总额";
    totalPriceItem.money = strTotalPrice;
    [self.dataArray addObject:totalPriceItem];
    
    
    
    if ([info objectForKey:@"postage"] ){
        self.postage = [info objectForKey:@"postage"];
        EFTransHomeDetailViewItemPrice* itemPostage = [[EFTransHomeDetailViewItemPrice alloc] init];
        itemPostage.name = @"运费";
        itemPostage.money = [NSString stringWithFormat:@"+¥%@", self.postage];
        [self.dataArray addObject:itemPostage];
       // realPay += self.postage.floatValue;
    }
    
    if ([info objectForKey:@"coupon"] ){
        NSString *coupon = [info objectForKey:@"coupon"];
        EFTransHomeDetailViewItemPrice* itemCoupon = [[EFTransHomeDetailViewItemPrice alloc] init];
        itemCoupon.name = @"优惠券";
        itemCoupon.money = [NSString stringWithFormat:@"-¥%@",coupon];
        [self.dataArray addObject:itemCoupon];
       // realPay -= coupon.floatValue;
    }
    
    if([info objectForKey:@"discount"]){
        self.discount = [info objectForKey:@"discount"];
    }
    
    if ([info objectForKey:@"deductAmount"] ){
        self.deductAmount = [info objectForKey:@"deductAmount"];
        EFTransHomeDetailViewItemPrice* itemDeductAmount = [[EFTransHomeDetailViewItemPrice alloc] init];
        itemDeductAmount.name = @"积分抵扣";
        itemDeductAmount.money = [NSString stringWithFormat:@"-¥%@",self.deductAmount];
        [self.dataArray addObject:itemDeductAmount];
       // realPay -= self.deductAmount.floatValue;
    }
    
    if([info objectForKey:@"payAmount"]){
        self.payAmount = [info objectForKey:@"payAmount"];
    }
    
    NSString* realPayStr = [NSString stringWithFormat:@"实付款￥%@",self.payAmount];
    
    NSMutableAttributedString * strIntro = [[NSMutableAttributedString alloc]initWithString:realPayStr];
    NSRange range = NSMakeRange([[strIntro string] rangeOfString:@"￥"].location, ([strIntro string].length - [[strIntro string] rangeOfString:@"￥"].location));
    [strIntro addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(0x60BA45) range:range];
    [self.totalPriceLabel setAttributedText:strIntro];
    [self.totalPriceLabel sizeToFit];
    self.totalPriceLabel.left = self.payBtn.left - self.totalPriceLabel.width - 10;
    self.totalPriceLabel.top = 14;
    
    [self.tableView reloadData];
}

-(void)extendArray{
    
    NSMutableIndexSet* indexSet = [[NSMutableIndexSet alloc]init];
    for(int i = 0 ; i < self.dataArray.count; ++i){
        ICTableViewItem* item = [self.dataArray objectAtIndex:i];
        if([item isKindOfClass:[EFOrderProductItem class]]){
            [indexSet addIndex:i];
        }else if([item isKindOfClass:[EFOrderOpenItem class]]){
            [indexSet addIndex:i];
        }
    }
    [self.dataArray removeObjectsAtIndexes:indexSet];
    
    for(int i = 0 ; i < self.goodsArray.count ; ++i){
        EFOrderProductItem* productItem = [self.goodsArray objectAtIndex:i];
        [self.dataArray insertObject:productItem atIndex: i+1];
    }
    
    [self.tableView reloadData];
    
}


-(void)updateShopCart{
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getCartListWithIsLocalDelivery:APP_CONTEXT.isLocalDelivery whCode:APP_CONTEXT.whCode type:0 OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.HUD hide:YES];
        if (code == 0) {
            NSArray *list = [info valueForKey:@"list"];
            NSArray *naList = [info valueForKey:@"naList"];
            
            [APP_CONTEXT.shoppingCartManager.storeProductsCart removeAllObjects];
            
            
            if (list.count == 0 && naList.count == 0) {
            }else{
                weakSelf.bottomView.hidden = NO;
                for (NSDictionary *dic in list) {
                    EFShoppingCartItem *item = [EFShoppingCartItem yy_modelWithDictionary:dic];
                    item.type = [NSNumber numberWithInteger:0];
                    [weakSelf.dataArray addObject:item];
                    
                     [APP_CONTEXT.shoppingCartManager addToStoreProduct:[item castToSpecialItem ]];
                }
            }
        }
    }];
}



@end
